package com.hawthorn.admin.controller.sysrole;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.service.sysrole.SysRoleService;
import com.hawthorn.platform.annotation.MarkLog;
import com.hawthorn.platform.exception.BizCode;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.platform.utils.iassert.AssertMyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 系统角色controller</p>
 *
 * @version v1.0.1
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/7/29 10:23
 * @copyright: Copyright (c)  andyten
 */
@Api(tags = {"admin-sysrole"})
@RestController
@RequestMapping("/sysrole/")
@Slf4j
public class SysRoleController
{
  @Resource
  SysRoleService sysRoleService;

  /**
   * <p>todo 查询角色</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/29 10:42
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询->查询角色信息", notes = "查询角色信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "查询条件", dataTypeClass = String.class)
  })
  @PostMapping(value = "/findRoles")
  public IPage<Map<String, Object>> findRoles(@RequestBody String params)
  {
    Page<Map<String, Object>> page = new Page<>();
    String conditions = MyUtil.getConditionsWhere(params, page);
    return sysRoleService.findRoles(page, conditions);
  }

  /**
   * <p>todo 查询渠道角色</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/29 10:42
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询->查询渠道角色信息", notes = "查询渠道角色信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "查询条件", dataTypeClass = String.class)
  })
  @PostMapping(value = "/findDealerRoles")
  public IPage<Map<String, Object>> findDealerRoles(@RequestBody String params)
  {
    Page<Map<String, Object>> page = new Page<>();
    String conditions = MyUtil.getConditionsWhere(params, page);
    return sysRoleService.findDealerRoles(page, conditions);
  }

  /**
   * <p>todo 新增角色</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/29 14:10
   * @version: 1.0.1
   */
  @ApiOperation(value = "新增 -> 新增角色信息", notes = "新增角色信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "param", value = "角色信息", dataTypeClass = String.class)
  })
  @MarkLog(value = "新增角色-[{0}]", param = "rcode")
  @PostMapping(value = "/saveRole")
  public Map<String, Object> saveRole(@RequestBody Map<String, Object> param)
  {
    return sysRoleService.saveRole(param);
  }

  /**
   * <p>todo 新增渠道商角色</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/29 14:10
   * @version: 1.0.1
   */
  @ApiOperation(value = "新增 -> 新增渠道商角色信息", notes = "新增渠道商角色信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "param", value = "渠道商角色信息", dataTypeClass = String.class)
  })
  @MarkLog(value = "新增渠道商角色-[{0}]", param = "rcode")
  @PostMapping(value = "/saveDealerRole")
  public Map<String, Object> saveDealerRole(@RequestBody Map<String, Object> param)
  {
    return sysRoleService.saveDealerRole(param);
  }

  /**
   * <p>todo 更新角色</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/29 14:11
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新 -> 更新角色信息", notes = "更新角色信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class),
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "更新角色-[{0}]", param = "rcode")
  @PutMapping(value = "/updateRole/{id}")
  public Map<String, Object> updateRole(@PathVariable("id") Long id, @RequestBody Map<String, Object> param)
  {
    return sysRoleService.updateRole(id, param);
  }

  /**
   * <p>todo 更新渠道商角色</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/29 14:11
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新 -> 更新渠道商角色信息", notes = "更新渠道商角色信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class),
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "更新渠道角色-[{0}]", param = "rcode")
  @PutMapping(value = "/updateDealerRole/{id}")
  public Map<String, Object> updateDealerRole(@PathVariable("id") Long id, @RequestBody Map<String, Object> param)
  {
    
    return sysRoleService.updateDealerRole(id, param);
  }

  /**
   * <p>todo 更新角色状态</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/29 20:57
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新 -> 更新角色状态", notes = "更新角色状态")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @MarkLog(value = "更新角色状态-[{0}]", param = "rcode")
  @PostMapping(value = "/updateRoleStatus")
  public Map<String, Object> updateRoleStatus(@RequestBody Map<String, Object> params)
  {
    Long id = Long.parseLong(MyUtil.getValue(params.get("id")));
    Integer status = Integer.parseInt(MyUtil.getValue(params.get("status")));
    return sysRoleService.updateRoleStatus(id, status);
  }

  /**
   * <p>todo 授予角色权限</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/30 20:09
   * @version: 1.0.1
   */
  @ApiOperation(value = "保存 -> 授予角色权限", notes = "授予角色权限")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @MarkLog(value = "授予角色权限-[{0}]", param = "rcode")
  @PostMapping(value = "/saveAuthGrantMenus")
  public Map<String, Object> saveAuthGrantMenus(@RequestBody List<Map<String, Object>> params)
  {
    AssertMyUtil.notNull(params, BizCode.METHOD_ARGS_NOTNULL, "params");
    Map<String, Object> ret = new HashMap<>();
    String roleType = "";
    if (params.size() > 0)
    {
      ret.put("rcode", params.get(0).get("roleCode"));
      roleType = MyUtil.getValue(params.get(0).get("type"));
    }
    sysRoleService.saveAuthGrantMenus(params);

    return ret;
  }

  /**
   * <p>todo 查询已授权的菜单信息</p>
   *
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/30 20:10
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询 -> 已授权的菜单信息", notes = "已授权的菜单信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "roleId", value = "roleId", dataTypeClass = Long.class)
  })
  @GetMapping(value = "/searchAuthGrantMenus")
  public List<Map<String, Object>> searchAuthGrantMenus(@RequestParam Long roleId)
  {
    AssertMyUtil.notNull(roleId, BizCode.METHOD_ARGS_NOTNULL, "roleId");
    return sysRoleService.searchAuthGrantMenus(roleId);
  }
}
